package com.heimali.sf.utils;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Environment;
import android.util.DisplayMetrics;
import android.view.WindowManager;

import com.heimali.sf.SiFaApplication;
import com.heimali.sf.config.Constants;

public class SystemUtils {
	private static PackageManager pm = null;
	private static String versionName = null;
	private static int versionCode = 0;
	private static float density = 0;
	private static int screenWidth = 0;
	private static int screenHeight = 0;
	
	/**
	 * 终端屏幕分辨率
	 */
	public int baseScreenWidth = -1, baseScreenHeight = -1;
	/**
	 * 密度
	 */
	public float baseDensity = -1;
	
	public static void Log(Object... logs) {
		if (!Constants.IS_DEBUG) {
			return ;
		}
		
		String out = "";
		for(Object log : logs) {
			if (log == null) {
				out += "null , ";
			}else {
				out += log.toString() + " , ";
			}
		}
		System.out.println(out.substring(0, out.length()-3));
	}
	
	public static PackageManager getPackageManager() {
		if (pm == null)
			pm = SiFaApplication.getApplication().getBaseContext().getPackageManager();
		return pm;
	}

	public static PackageInfo getPackageInfo(String packageName) {
		try {
			if (packageName == null)
				packageName = SiFaApplication.getContext().getPackageName();
			return getPackageManager().getPackageInfo(packageName, 0);
		} catch (NameNotFoundException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static int getVersionCode() {
		if (versionCode == 0) {
			versionCode = getPackageInfo(null).versionCode;
		}
		return versionCode;
	}
	

	public static String getVersionName() {
		if (versionName == null) {
			versionName = getPackageInfo(null).versionName;
		}
		return versionName;
	}

	public static String getModel() {
		return android.os.Build.MODEL;
	}
	
	public static boolean isMounted() {
		return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
	}
	
	public static String getSDCardPath() {
		return Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator;
	}
	
	private static void getScreenInfo() {
		DisplayMetrics dm = new DisplayMetrics();
		((WindowManager) SiFaApplication.getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(dm);
		density = dm.density;
		screenHeight = dm.heightPixels;
		screenWidth = dm.widthPixels;
	}
	
	public static float getDensity() {
		if (density == 0) {
			getScreenInfo();
		}
		return density;
	}
	
	public static int getScreenWidth() {
		if (screenWidth == 0) {
			getScreenInfo();
		}
		return screenWidth;
	}
	
	public static int getScreenHeight() {
		if (screenHeight == 0) {
			getScreenInfo();
		}
		return screenHeight;
	}
	
	public static String getCurrentTime() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date curDate = new Date(System.currentTimeMillis());
		return formatter.format(curDate);
	}
	
	public static String getCurrentTime(Date curDate) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return formatter.format(curDate);
	}
	
	
	public static String getCurrentDate() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		Date curDate = new Date(System.currentTimeMillis());
		return formatter.format(curDate);
	}
	
	public static String getCurrentDate(Date curDate) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		return formatter.format(curDate);
	}
	
	public static String getCurrentMinuteTime() {
		SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");
		Date curDate = new Date(System.currentTimeMillis());
		return formatter.format(curDate);
	}
	
	public static int getCurrentYear() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy");
		Date curDate = new Date(System.currentTimeMillis());
		return Integer.valueOf(formatter.format(curDate));
	}
	
	public static int getCurrentMonth() {
		SimpleDateFormat formatter = new SimpleDateFormat("MM");
		Date curDate = new Date(System.currentTimeMillis());
		return Integer.valueOf(formatter.format(curDate));
	}
	
	public static int getCurrentDay() {
		SimpleDateFormat formatter = new SimpleDateFormat("dd");
		Date curDate = new Date(System.currentTimeMillis());
		return Integer.valueOf(formatter.format(curDate));
	}
	
	public static int getCurrentHour() {
		SimpleDateFormat formatter = new SimpleDateFormat("HH");
		Date curDate = new Date(System.currentTimeMillis());
		return Integer.valueOf(formatter.format(curDate));
	}
	
	public static int getCurrentMinute() {
		SimpleDateFormat formatter = new SimpleDateFormat("mm");
		Date curDate = new Date(System.currentTimeMillis());
		return Integer.valueOf(formatter.format(curDate));
	}
	
	public static long getTimeStamp(String time) {
		long res = 0;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date d;
		try {
			d = sdf.parse(time);
			res = d.getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return res;
	}
}
